package com.example.home.attorney;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.home.R;
import com.example.home.attorney.activity.AttorneyDetail;
import com.example.home.attorney.activity.MeConsult;
import com.example.home.attorney.activity.MoreAttorneyList;
import com.example.home.attorney.adapter.HotAttorneyAdapter;
import com.example.home.attorney.adapter.LawyerBannerAdapter;
import com.example.home.attorney.adapter.LawyerCardListAdapter;
import com.example.home.attorney.bean.AttorneySearchBean;
import com.example.home.attorney.bean.HotAttorneyBean;
import com.example.home.attorney.bean.LawyerBannerBean;
import com.example.home.attorney.bean.LawyerCardListBean;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class AttorneyMainActivity extends AppCompatActivity {
    private ImageView back;
    private SearchView seLawyer;
    private ViewPager vpBanner;
    private RecyclerView rcvList;
    private LinearLayout liCenter;
    private RecyclerView reLushiList;
    private Button btnMore;
    private Timer timer;
    private int currentPage = 0;
    private final long DELAY_MS = 1000;
    private final long PERIOD_MS = 3000;
    private LawyerBannerAdapter lawyerBannerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attorney_main);
        initView();

        
        //轮播图
        getBanner();
        startAutoScroll();

        getCardList();

        getAttorneyList();
    }


    private void getAttorneyList() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/lawyer/list-top10",
                OKHttpTool.GET,
                HotAttorneyBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        HotAttorneyBean hotAttorneyBean = (HotAttorneyBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindList(hotAttorneyBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindList(HotAttorneyBean hotAttorneyBean) {
        HotAttorneyAdapter hotAttorneyAdapter = new HotAttorneyAdapter(AttorneyMainActivity.this, hotAttorneyBean.getData());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(AttorneyMainActivity.this);
        reLushiList.setAdapter(hotAttorneyAdapter);
        reLushiList.setLayoutManager(linearLayoutManager);
    }

    private void getCardList() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/legal-expertise/list",
                OKHttpTool.GET,
                LawyerCardListBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LawyerCardListBean lawyerCardListBean = (LawyerCardListBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                LawyerCardListAdapter lawyerCardListAdapter = new LawyerCardListAdapter(AttorneyMainActivity.this, lawyerCardListBean.getRows());
                                rcvList.setAdapter(lawyerCardListAdapter);
                                GridLayoutManager gridLayoutManager = new GridLayoutManager(AttorneyMainActivity.this, 2);
                                gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
                                rcvList.setLayoutManager(gridLayoutManager);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }


    private void getBanner() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/ad-banner/list",
                OKHttpTool.GET,
                LawyerBannerBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LawyerBannerBean lawyerBannerBean = (LawyerBannerBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindBannershow(lawyerBannerBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindBannershow(LawyerBannerBean lawyerBannerBean) {
        ViewPager viewPager = findViewById(R.id.vp_banner);
        lawyerBannerAdapter = new LawyerBannerAdapter(AttorneyMainActivity.this, lawyerBannerBean.getData());
        viewPager.setAdapter(lawyerBannerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void startAutoScroll() {
        final Handler handler = new Handler();
        final Runnable update = new Runnable() {
            @Override
            public void run() {
                if (currentPage == lawyerBannerAdapter.getCount()) {
                    currentPage = 0;
                }
                vpBanner.setCurrentItem(currentPage++, true);
                ;
            }
        };
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(update);
            }
        }, DELAY_MS, PERIOD_MS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }


    private void initView() {

        back = findViewById(R.id.back);
        seLawyer = findViewById(R.id.se_lawyer);
        vpBanner = findViewById(R.id.vp_banner);
        rcvList = findViewById(R.id.rcv_list);
        liCenter = findViewById(R.id.li_center);
        reLushiList = findViewById(R.id.re_lushiList);
        btnMore = findViewById(R.id.btn_more);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AttorneyMainActivity.this.finish();
            }
        });

        btnMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AttorneyMainActivity.this, MoreAttorneyList.class);
                startActivity(intent);
            }
        });

        seLawyer.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // 在用户提交查询时调用
                performSearch(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        liCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AttorneyMainActivity.this, MeConsult.class);
                startActivity(intent);
            }
        });

    }

    private void performSearch(String query) {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/lawyer/list",
                OKHttpTool.GET,
                AttorneySearchBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        AttorneySearchBean attorneySearchBean = (AttorneySearchBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // 在数据集合中进行模糊匹配
                                List<AttorneySearchBean.RowsBean> matchedList = new ArrayList<>();
                                for (AttorneySearchBean.RowsBean item : attorneySearchBean.getRows()) {
                                    if (item.getName().toLowerCase().contains(query.toLowerCase())) {
                                        matchedList.add(item);
                                    }
                                }

                                // 如果有匹配项，可以选择展示在界面上，这里以直接跳转为例
                                if (!matchedList.isEmpty()) {
                                    // 如果只有一个匹配项，直接跳转
                                    if (matchedList.size() == 1) {
                                        navigateToDetailActivity(matchedList.get(0));
                                    } else {
                                        // 如果有多个匹配项，可以展示在界面上供用户选择
                                        // 你可以使用 RecyclerView 或其他方式展示匹配项列表
                                        // 然后在用户选择后再跳转
                                    }
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void navigateToDetailActivity(AttorneySearchBean.RowsBean rowsBean) {
        // 根据匹配项跳转到相应的 Activity
        Intent intent = new Intent(AttorneyMainActivity.this, AttorneyDetail.class);
        Gson gson = new Gson();
        intent.putExtra("attorney", gson.toJson(rowsBean));
        // 添加其他需要传递的数据
        startActivity(intent);
    }
}